package com.touch.core.script;
/**
 * The ByteArray class exists as an array of bytes with extended modification values.
 */
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public final class ByteArray extends ByteEncoder
{
	private byte[] byteArray;
	/** Returns the size of the byte array */
	public final int length()
	{
		return byteArray.length;
	}
	/** Creates a new byte array */
	public ByteArray()
	{
		byteArray = new byte[0];
	}
	/**
	 * Creates a new byte array from an array of bytes.
	 * @param newArray Array of bytes to make the array from.
	 */
	public ByteArray(byte[] newArray)
	{
		byteArray = newArray;
	}	
	public final void appendTo(ByteArray newByteArray)
	{
		appendTo(newByteArray.toArray());
	}
	/**
	 * Adds a boolean to the end of the byte array
	 * @param newBool Boolean to add
	 */
	public final void appendTo(boolean newBool)
	{
		final byte[] newArray;
		newArray = booleanToByteArray(newBool);
		byteArray = appendToByteArray(byteArray, newArray);
	}
	/**
	 * Adds a byte to the end of the byte array
	 * @param newByte Byte to add
	 */
	public final void appendTo(byte newByte)
	{
		final byte[] newArray = new byte[1];
		newArray[0] = newByte;
		byteArray	= appendToByteArray(byteArray, newArray);
		
	}
	/**
	 * Adds an integer to the end of the byte array
	 * @param newInt Integer to add
	 */
	public final void appendTo(int newInt)
	{
		final byte[] newArray;
		newArray 	= intToByteArray(newInt);
		byteArray	= appendToByteArray(byteArray, newArray);
	}
	/**
	 * Adds a long to the end of a byte array
	 * @param newLong Long to add
	 */
	public final void appendTo(long newLong)
	{
		byte[] newArray;
		newArray	= longToByteArray(newLong);
		byteArray	= appendToByteArray(byteArray, newArray);
	}
	/**
	 * Adds a float to the end of a byte array
	 * @param newFloat Float to add
	 */
	public final void appendTo(float newFloat)
	{
		final byte[] newArray;
		newArray	= floatToByteArray(newFloat);
		byteArray	= appendToByteArray(byteArray, newArray);
	}
	/**
	 * Adds a double to the end of a byte array
	 * @param newDouble Double to add
	 */
	public final void appendTo(double newDouble)
	{
		final byte[] newArray;
		newArray	= doubleToByteArray(newDouble);
		byteArray	= appendToByteArray(byteArray, newArray);
	}
	/**
	 * Adds a String to the end of a byte array
	 * @param newString String to add
	 */
	public final void appendTo(String newString)
	{
		final byte[] newArray;
		newArray	= stringToByteArray(newString);
		byteArray	= appendToByteArray(byteArray, newArray);
	}
	/**
	 * Adds a character to the end of a byte array
	 * @param newChar Char to add
	 */
	public final void appendTo(char newChar)
	{
		final byte[] newArray;
		newArray	= charToByteArray(newChar);
		byteArray	= appendToByteArray(byteArray, newArray);
	}
	/**
	 * Adds an array of bytes to the end of a byte array
	 * @param newArray Bytes to add
	 */
	public final void appendTo(byte[] newArray)
	{
		byteArray = appendToByteArray(byteArray, newArray);
	}
	/**
	 * Adds an array of bytes to a specified location of the byte array
	 * @param newArray Bytes to add
	 * @param offset Location to add to
	 */
	public final void appendTo(byte[] newArray, int offset)
	{
		byteArray = appendToByteArray(byteArray, newArray, offset);
	}	
	/**
	 * Returns a new ByteArray from a section of the ByteArray
	 * @param start Start location
	 * @param end End location
	 */
	public final ByteArray subArray(int start, int end)
	{
		return new ByteArray(getSubByteArray(byteArray, start, end));
	}
	/**
	 * Returns an array of bytes representing the byte array
	 * @return
	 */
	public final byte[] toArray()
	{
		return byteArray;
	}
	/**
	 * Converts the ByteArray into a Char or 16bit unsigned integer.
	 * This only wiByteInterpreterll work if the array's size is 2
	 * @return
	 */
	public final char toChar()
	{
		return byteArrayToChar(this.toArray());
	}
	/**
	 * Converts the ByteArray into a 32bit signed integer 
	 * This only works if the array's size is 4.
	 * @return The ByteArray in integer form
	 */
	public final int toInt()
	{
		return byteArrayToInt(this.toArray(), 0);
	}
	/**
	 * Converts the ByteArray into a 32bit signed integer
	 * This only works if the array's size is 8
	 * @return The ByteArray in long form
	 */
	public final long toLong()
	{
		return byteArrayToLong(this.toArray(), 0);
	}
	public final short toShort()
	{
		return byteArrayToShort(this.toArray(), 0);
	}
	public final float toFloat()
	{		
		return byteArrayToFloat(this.toArray(), 0);
	}
	public final double toDouble()
	{
		return byteArrayToDouble(this.toArray(), 0);
	}
	public final String toString()
	{
		return byteArrayToString(this.toArray());
	}
	public final byte toByte()
	{
		return this.toArray()[0];
	}
	/**
	 * Overwrites the byte array with an array of bytes
	 * @param newArray Array of bytes to overwrite the byte array
	 */
	public final void setArray(byte[] newArray)
	{
		byteArray = newArray;
	}	
}
